package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.ChuangShiRenDaoimpl;
import com.ysd.entity.ChuangShiRen;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminSheJiShiServlet extends BaseServlet {
	/**
	 *  localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=selSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
//	public void selSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
//		int page =Integer.parseInt(request.getParameter("page"));
//		
//		int size =Integer.parseInt(request.getParameter("size"));
//		ArrayList<ChuangShiRen> csr=csrDao.getChuagShiRen(page,size);
//		
//		response.setCharacterEncoding("UTF-8");
//		PrintWriter out=response.getWriter();
//		
//		Gson gson=new Gson();
//		out.print(gson.toJson(csr));
//		
//		out.flush();
//		out.close();
//	}
	
	
	/**
	 * 根据设计师id查询设计师的长度
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=selSheJiShiSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selSheJiShiSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		ArrayList<ChuangShiRen> csr=csrDao.getChuagShiRensize();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(csr.size()));
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=delSheJiShi
	 * 根据设计师id删除对应的数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		int sheJiShiId=Integer.parseInt(request.getParameter("sheJiShiId"));
		int  n=csrDao.delShiJiShi(sheJiShiId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(true, "删除失败", "您的操作有误"));
		}
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=addSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		String bossName=request.getParameter("bossName");
		String zhiWei=request.getParameter("zhiWei");
		String touXiang=request.getParameter("touXiang");
		String sex=request.getParameter("sex");
		
		
		
		int age=Integer.parseInt(request.getParameter("age"));
		
		
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		
		ArrayList<ChuangShiRen> csrList=csrDao.selSheJiShi(bossName);
		if(csrList.size() == 0) {
			if(age>0 && age<120) {
				ChuangShiRen csr=new ChuangShiRen(0, bossName, zhiWei, touXiang, sex, age);
				int  n=csrDao.addSheJiShi(csr);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				}else {
					out.print(Result.toClient(false, "添加失败", "您的操作有误"));
				}
			}else {
				out.print(Result.toClient(false, "您的年龄有误", "您的操作有误"));
			}
			
		}else {
			out.print(Result.toClient(false, "您输入的姓名已经存在", "您的操作有误"));
		}
		
		
		out.flush();
		out.close();
	}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=selSheJiShiWhere
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selSheJiShiWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		String bossName=request.getParameter("bossName");
		
		String zhiWei=request.getParameter("zhiWei");
		String touXiang=request.getParameter("touXiang");
		String sex=request.getParameter("sex");
		
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		ChuangShiRen csr=new ChuangShiRen(0, bossName, zhiWei, touXiang, sex, 0);
		
		

		int page =Integer.parseInt(request.getParameter("page"));
		
		int size =Integer.parseInt(request.getParameter("size"));
		ArrayList<ChuangShiRen> csrList=csrDao.selSheJiShiWhere(csr,page,size);
		
		Gson gson=new Gson();
		out.print(gson.toJson(csrList));
				
		out.flush();
		out.close();
}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=selSheJiShiWhereSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selSheJiShiWhereSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		String bossName=request.getParameter("bossName");
		
		String zhiWei=request.getParameter("zhiWei");
		String touXiang=request.getParameter("touXiang");
		String sex=request.getParameter("sex");
		
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		ChuangShiRen csr=new ChuangShiRen(0, bossName, zhiWei, touXiang, sex, 0);
		
		
		ArrayList<ChuangShiRen> csrList=csrDao.selSheJiShiWherepage(csr);
		Gson gson=new Gson();
		out.print(gson.toJson(csrList.size()));
				
		out.flush();
		out.close();
}
		/**
		 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=upload	
		 * @param request
		 * @param response
		 * @throws ServletException
		 * @throws IOException
		 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		SmartUpload smart=new  SmartUpload();
		smart.setCharset("UTF-8");
		smart.initialize(getServletConfig(), request, response);
		
		try {
			smart.upload();
			String name=System.currentTimeMillis()+".jpg";
			smart.getFiles().getFile(0).saveAs("/images/"+name);
			out.print(Result.toClient(true, "上传成功", name));
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		
		out.flush();
		out.close();
}		
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=getChuagShiRenById	
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChuagShiRenById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		int sheJiShiId=Integer.parseInt(request.getParameter("sheJiShiId"));
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		
		ChuangShiRen csr=csrDao.getChuagShiRenById(sheJiShiId);
		
		Gson gson=new Gson();
		out.print(gson.toJson(csr));
				
		out.flush();
		out.close();
}		
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminshejishi?reqName=editSheJiShiById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editSheJiShiById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		String bossName=request.getParameter("bossName");
		String zhiWei=request.getParameter("zhiWei");
		String touXiang=request.getParameter("touXiang");
		String sex=request.getParameter("sex");
		int bossId=Integer.parseInt(request.getParameter("bossId"));
		int age=Integer.parseInt(request.getParameter("age"));
		
		
		ChuangShiRenDaoimpl csrDao=new ChuangShiRenDaoimpl();
		
		ArrayList<ChuangShiRen> csrList=csrDao.selSheJiShi(bossName);
		if(csrList.size() == 0) {
			if(age>0 && age<120) {
				ChuangShiRen csr=new ChuangShiRen(bossId, bossName, zhiWei, touXiang, sex, age);
				int  n=csrDao.editSheJiShiById(csr);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功", ""));
				}else {
					out.print(Result.toClient(false, "修改失败", "您的操作有误"));
				}
			}else {
				out.print(Result.toClient(false, "您的年龄有误", "您的操作有误"));
			}
			
		}else {
			out.print(Result.toClient(false, "姓名已经存在", "您的操作有误"));
		}
		
		
		out.flush();
		out.close();
	}
		

}
